Public Class frmManageCongenitalDisease

#Region "Declare Variables"
    Private m_eScreenMode As eScreenMode
    Private m_CongenitalDisease As New PoliceHealthClass.CongenitalDisease
    Private m_ManageCongenitalDiseaseControl As New PoliceHealthClass.ManageReferrenceControl
    Private IsPostBack As Boolean = False

    Private cbbStoolExamResultHistory As ComboBox

    Delegate Sub delLoadDatagridviewCongenitalDisease(ByVal intSelectRowIndex As Integer)
#End Region

#Region "Enumuration"
    Private Enum eScreenMode As Integer
        Initial
        View
        Add
        Edit
    End Enum
#End Region

#Region "General Functions"
    Private Sub SetScreenMode(ByVal ScreenMode As eScreenMode)
        m_eScreenMode = ScreenMode
        Select Case ScreenMode
            Case eScreenMode.Initial
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, btnDelete, btnEdit, txtCongenitalDiseaseNameTha, txtCongenitalDiseaseNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnAdd)

                dgvCongenitalDisease.Enabled = True

                If Not Me.IsPostBack Then

                    ConnectionString.ShowLoadingForm()
                    Me.Invoke(New delLoadDatagridviewCongenitalDisease(AddressOf LoadDatagridviewCongenitalDisease), 0)
                    ConnectionString.TerminateLoadingForm()

                    'LoadDatagridviewCongenitalDisease()
                    Me.IsPostBack = True
                End If

            Case eScreenMode.View
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, txtCongenitalDiseaseNameTha, txtCongenitalDiseaseNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnEdit, btnDelete, btnAdd)

                dgvCongenitalDisease.Enabled = True
            Case eScreenMode.Add

                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtCongenitalDiseaseNameTha, txtCongenitalDiseaseNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                ClearFormData()
                dgvCongenitalDisease.Enabled = False

            Case eScreenMode.Edit
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtCongenitalDiseaseNameTha, txtCongenitalDiseaseNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                dgvCongenitalDisease.Enabled = False
        End Select


    End Sub


    Private Function VerifyInputData() As Boolean
        Return m_ManageCongenitalDiseaseControl.verifyInsert()
    End Function

    Private Sub LoadDatagridviewCongenitalDisease(Optional ByVal intSelectRowIndex As Integer = 0)
        dgvCongenitalDisease.Enabled = True
        m_CongenitalDisease.LoadCongenitalDiseaseAll()

        Dim intRowCount As Integer = 0
        dgvCongenitalDisease.Rows.Clear()
        While m_CongenitalDisease.NextRecord
            dgvCongenitalDisease.Rows.Add()
            dgvCongenitalDisease.Rows(intRowCount).Cells("Order").Value = intRowCount + 1
            dgvCongenitalDisease.Rows(intRowCount).Cells("CongenitalDiseaseID").Value = m_CongenitalDisease.congenital_disease_id
            dgvCongenitalDisease.Rows(intRowCount).Cells("CongenitalDiseaseNameThai").Value = m_CongenitalDisease.congenital_disease_name_tha
            dgvCongenitalDisease.Rows(intRowCount).Cells("CongenitalDiseaseNameEng").Value = m_CongenitalDisease.congenital_disease_name_eng


            dgvCongenitalDisease.Rows(intRowCount).Selected = False


            intRowCount += 1
        End While

        If intSelectRowIndex < dgvCongenitalDisease.Rows.Count Then
            dgvCongenitalDisease.Rows(intSelectRowIndex).Selected = True
            dgvCongenitalDisease.CurrentCell = dgvCongenitalDisease.Rows(intSelectRowIndex).Cells(0)
        End If

        If dgvCongenitalDisease.Rows.Count > 0 Then
            SetScreenMode(eScreenMode.View)
            LoadDataForm(dgvCongenitalDisease.Rows(intSelectRowIndex).Cells("CongenitalDiseaseID").Value)
        Else
            If Me.IsPostBack Then
                SetScreenMode(eScreenMode.Initial)
            End If
            ClearFormData()
        End If
    End Sub

    Private Sub LoadDataForm(ByVal CongenitalDiseaseID As String)
        txtCongenitalDiseaseID.Text = CongenitalDiseaseID

        'Load Form
        If dgvCongenitalDisease.SelectedRows.Count > 0 Then
            txtCongenitalDiseaseNameTha.Text = dgvCongenitalDisease.SelectedRows(0).Cells("CongenitalDiseaseNameThai").Value
            txtCongenitalDiseaseNameEng.Text = dgvCongenitalDisease.SelectedRows(0).Cells("CongenitalDiseaseNameEng").Value
        Else
            txtCongenitalDiseaseNameTha.Text = dgvCongenitalDisease.Rows(0).Cells("CongenitalDiseaseNameThai").Value
            txtCongenitalDiseaseNameEng.Text = dgvCongenitalDisease.Rows(0).Cells("CongenitalDiseaseNameEng").Value
        End If

    End Sub

    Private Sub ClearFormData()
        txtCongenitalDiseaseNameTha.Text = ""
        txtCongenitalDiseaseNameEng.Text = ""
        PoliceHealthClass.Utilities.EnableControl(False, btnEdit)
    End Sub






#End Region

#Region "Event Handling Functions"
    Private Sub frmPolice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CMU_KPI_PHASE_1DataSet.tbError_Logging' table. You can move, or remove it, as needed.
        SetScreenMode(eScreenMode.Initial)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If dgvCongenitalDisease.SelectedRows.Count > 0 Then
            LoadDataForm(dgvCongenitalDisease.Rows(0).Cells("CongenitalDiseaseID").Value)
            SetScreenMode(eScreenMode.View)
        Else
            SetScreenMode(eScreenMode.Initial)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim SelectIndex As Integer = 0
        Dim blVerifyInputControl As Boolean = False


        If m_eScreenMode = eScreenMode.Edit OrElse m_eScreenMode = eScreenMode.Add Then
            If VerifyInputData() Then

                If dgvCongenitalDisease.SelectedRows.Count > 0 Then
                    SelectIndex = dgvCongenitalDisease.SelectedRows(0).Index
                End If


                m_CongenitalDisease.congenital_disease_id = PoliceHealthClass.Utilities.ClearEmptyToMinValue(txtCongenitalDiseaseID.Text, TypeCode.Int32)
                m_CongenitalDisease.congenital_disease_name_tha = txtCongenitalDiseaseNameTha.Text
                m_CongenitalDisease.congenital_disease_name_eng = txtCongenitalDiseaseNameEng.Text



                If m_eScreenMode = eScreenMode.Edit Then
                    m_ManageCongenitalDiseaseControl.showInsertResult(m_CongenitalDisease.updateCongenitalDisease(m_CongenitalDisease))
                ElseIf m_eScreenMode = eScreenMode.Add Then
                    m_ManageCongenitalDiseaseControl.showInsertResult(m_CongenitalDisease.insertCongenitalDisease(m_CongenitalDisease))
                End If

                ConnectionString.ShowLoadingForm()
                Me.Invoke(New delLoadDatagridviewCongenitalDisease(AddressOf LoadDatagridviewCongenitalDisease), dgvCongenitalDisease.SelectedRows(0).Index)
                ConnectionString.TerminateLoadingForm()

                'LoadDatagridviewCongenitalDisease(dgvCongenitalDisease.SelectedRows(0).Index)

            End If
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        SetScreenMode(eScreenMode.Add)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        SetScreenMode(eScreenMode.Edit)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If m_ManageCongenitalDiseaseControl.verifyDelete() Then
            m_CongenitalDisease.congenital_disease_id = txtCongenitalDiseaseID.Text
            m_ManageCongenitalDiseaseControl.showDeleteResult(m_CongenitalDisease.deleteCongenitalDisease(m_CongenitalDisease))

            ConnectionString.ShowLoadingForm()
            Me.Invoke(New delLoadDatagridviewCongenitalDisease(AddressOf LoadDatagridviewCongenitalDisease), 0)
            ConnectionString.TerminateLoadingForm()

            'LoadDatagridviewCongenitalDisease()
        End If
    End Sub

    Private Sub dgvCongenitalDisease_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCongenitalDisease.RowEnter
        LoadDataForm(dgvCongenitalDisease.Rows(e.RowIndex).Cells("CongenitalDiseaseID").Value)
    End Sub
#End Region
End Class